package com.oceanstar.springboot.service.impl;

import com.oceanstar.springboot.mapper.mapper;
import com.oceanstar.springboot.pojo.entity.Student;
import com.oceanstar.springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;


@Service
public class StudentServiceImpl  implements StudentService {
    @Autowired
    private mapper mapper;

    /**
     * 查询所有学生信息
     * @return
     */
    public List<Student> hello() {
        return mapper.hello();
    }
    
    /**
     * 根据ID查询学生
     * @param id
     * @return
     */
    @Override
    public Student getStudentById(Integer id) {
        return mapper.getStudentById(id);
    }
    
    /**
     * 添加学生
     * @param student
     */
    @Override
    public void addStudent(Student student) {
        // 如果ID为null，则生成一个新的唯一ID
        if (student.getId() == null) {
            student.setId(generateUniqueId());
        }
        mapper.addStudent(student);
    }
    
    /**
     * 更新学生信息
     * @param student
     */
    @Override
    public void updateStudent(Student student) {
        mapper.updateStudent(student);
    }
    
    /**
     * 删除学生
     * @param id
     */
    @Override
    public void deleteStudent(Integer id) {
        mapper.deleteStudent(id);
    }
    
    /**
     * 生成唯一的ID
     * @return
     */
    private Integer generateUniqueId() {
        // 简单的ID生成策略：使用随机数+时间戳的后几位
        Random random = new Random();
        int randomNum = random.nextInt(1000);
        int timestamp = (int) (System.currentTimeMillis() % 100000);
        return randomNum * 100000 + timestamp;
    }


}